package src.manager
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	import src.event.EventAnj;
	
	/**
	 * 声音播放管理类
	 * @author anjue
	 */
	public class SoundManager
	{
		public static var playEnable:Boolean = true;
		
		private static var soundFactory:Sound;
		private static var _channel:SoundChannel;
		private static var _position:Number = 0;
		private static var _pauseBool:Boolean = false;
		private static var _volume:Number = -1;
		
		public static var ownEvent:EventDispatcher = new EventDispatcher();
		public static function play(url:String):void
		{
			if(playEnable && !_pauseBool)
			{
				if(_channel)
				{
					_channel.stop();
					_channel = null;
					destroy();
				}
				var request:URLRequest = new URLRequest(url);
	            soundFactory = new Sound();
	            soundFactory.addEventListener(IOErrorEvent.IO_ERROR,error);
	            soundFactory.load(request);
	            _channel = soundFactory.play();
	            if(_volume != -1)
	            {
	            	var st:SoundTransform = new SoundTransform(_volume);
					_channel.soundTransform = st;
	            }
	 		 }else if(playEnable && _pauseBool)
	 		 {
	 		 	if(soundFactory) soundFactory.play(_position);
	 		 }
		}
		
		/**
		 * 检测MP3文档是否存在
		 * @param $url
		 */
		public static function checkMp3Exist($url:String):void
		{
			var sf:Sound = new Sound();
			sf.addEventListener(Event.COMPLETE,checkLoadComplete);
            sf.addEventListener(IOErrorEvent.IO_ERROR,checkError);
            sf.load(new URLRequest($url));
		}
		
		private static function checkLoadComplete(e:Event):void
		{
			var s:Sound = e.target as Sound;
			s.removeEventListener(Event.COMPLETE,checkLoadComplete);
			s.removeEventListener(IOErrorEvent.IO_ERROR,checkError);
			
//			PMI.title.text += "success"+s.url+"\n";
			var url:String = "resource" + s.url.split("resource")[1];
			ownEvent.dispatchEvent(new EventAnj(Event.COMPLETE,{"flag":1,"url":url}));
		}
		
		private static function checkError(e:Event):void
		{
			var s:Sound = e.target as Sound;
			s.removeEventListener(Event.COMPLETE,checkLoadComplete);
			s.removeEventListener(IOErrorEvent.IO_ERROR,checkError);
			
//			PMI.title.text += "error"+s.url+"\n";
			var url:String = "resource" + s.url.split("resource")[1];
			ownEvent.dispatchEvent(new EventAnj(Event.COMPLETE,{"flag":0,"url":url}));
		}
		
		/**
		 * 暂停
		 */
		public static function pause():void
		{
			if(_channel)
			{
				_position = _channel.position;
				_channel.stop();
				_pauseBool = true;
			}
		}
		
		/**
		 * 停止播放声音
		 */
		public static function stop():void
		{
			if(_channel)
			{
				_channel.stop();
				_position = 0;
				_pauseBool = false;
			}
		}
		
		public static function set volume(v:Number):void
		{
			var _volume:Number = v;
			if(_channel)
			{
				var st:SoundTransform = new SoundTransform(v);
				_channel.soundTransform = st;
			}
		}
		
		public static function get volume():Number
		{
			if(_channel)
				_volume = _channel.soundTransform.volume;
			return _volume;
		}
		
		/**
		 * 銷毀聲音
		 */
		public static function destroy():void
		{
			_volume = -1;
			_position = 0;
			_pauseBool = false;
			if(_channel)
			{
				_channel.stop();
				_channel = null;
			}
			
			try
			{
				soundFactory.close();
				soundFactory.removeEventListener(IOErrorEvent.IO_ERROR,error);
				soundFactory = null;
			}catch(e:Error)
			{
				trace(e);
			}
		}
		
		private static function error(e:IOErrorEvent):void
		{
			soundFactory.removeEventListener(IOErrorEvent.IO_ERROR,error);
			trace("加载声音文件失败----："+e.currentTarget.url);
		}
	}
}